package net.pulseproject.commons.midi.entity;

public class Device {
	
	private final String portName;
	
	private final int maxReceivers;
	
	private final int maxTransmitters;
	
	private final DeviceState state;
	
	
	public Device(final String portName,
				  final int maxReceivers,
				  final int maxTransmitters,
				  final DeviceState state) {
		if(portName == null) { throw new IllegalArgumentException("portName == null"); }
		if(portName.isEmpty()) { throw new IllegalArgumentException("portName is empty"); }
		this.portName = portName;
		
		this.maxTransmitters = maxTransmitters;
		this.maxReceivers = maxReceivers;

		if(state == null) { throw new IllegalArgumentException("state == null"); }
		if(DeviceState.byMaxReceiversTransmitters(maxReceivers, maxTransmitters) != state) {
			throw new IllegalArgumentException("Invalid state [" + state + "] for maxReceivers " +
					"[" + maxReceivers + "] and maxTransmitters [" + maxTransmitters + "]!");
		}
		this.state = state;
	}
	
	
	public final String getPortName() {
		return this.portName;
	}
	
	public final int getMaxTransmitters() {
		return this.maxTransmitters;
	}

	public final int getMaxReceivers() {
		return this.maxReceivers;
	}

	public final DeviceState getState() {
		return this.state;
	}
	
	
	@Override
	public final boolean equals(final Object other) {
		if((other instanceof Device) == false) {
			return false;
		}
		
		final Device that = (Device) other;
		return this.portName.equals(that.portName) &&
			   this.state == that.state &&
			   this.maxReceivers == that.maxReceivers &&
			   this.maxTransmitters == that.maxTransmitters;
	}

	@Override
	public final int hashCode() {
		return this.portName.hashCode() + this.maxReceivers;
	}
	
	@Override
	public final String toString() {
		return "Device[portName=[" + this.portName + "], state=" + this.state + "]";
	}
	
}
